package com.meixiang.main;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.meixiang.R;
import com.meixiang.view.TitleView;
import com.meixiang.view.statusView.StatusView;
import com.meixiang.view.swipetoloadlayout.base.SwipeToLoadLayout;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;

/**
 * 作者：黑哥 on 2016/3/29 09:54
 */
public abstract class BaseFragment extends Fragment{
    //title
    protected TitleView titleView;

    protected String TAG=this.getClass().getSimpleName();

    protected BaseActivity activity;

    //调用下面方法显示布局
    private StatusView status;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }

    protected   View rootView;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activity= (BaseActivity) getActivity();

    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return super.onCreateView(inflater,container,savedInstanceState);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        this.rootView=view;
        //getChildFragmentManager().beginTransaction().add(view.getId(),new ProjectFragment());
        initTitle();
        initView();
        setData();

    }
    //记录原来view的状态
    private int paddingLeft,paddingTop,paddingRight,paddingBottom;
    protected void initStatus(){
        if (status==null){
            status=new StatusView(activity);
            status.hintLineView();
        }
        if (status.getParent()==null){
            final ViewGroup group= (ViewGroup) rootView;
            //保存参数状态
            paddingLeft=group.getPaddingLeft();
            paddingTop=group.getPaddingTop();
            paddingRight=group.getPaddingRight();
            paddingBottom=group.getPaddingBottom();

            group.setPadding(0, 0, 0, 0);
            group.post(new Runnable() {
                @Override
                public void run() {

                    ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(group.getWidth(), group.getHeight());
                    for (int i = 0; i < group.getChildCount(); i++) {
                        group.getChildAt(i).setVisibility(View.GONE);
                    }
                    if (status.getParent()!=null){
                        status.removeAttachView();
                    }
                    group.addView(status, layoutParams);
                }
            });

        }

    }

    /**
     * 清除状态布局
     */
    protected void removeStatus(){
        if (status==null){
            return;
        }
        status.removeAttachView();
        ViewGroup group= (ViewGroup) rootView;
        //rootView.setLayoutParams(oldParams);
        for (int i=0;i<group.getChildCount();i++){
            group.getChildAt(i).setVisibility(View.VISIBLE);
        }
        group.setPadding(paddingLeft,paddingTop,paddingRight,paddingBottom);
    }
    /**
     * 全屏显示无数据状态,无点击事件
     */
    protected void showNoData(String message){
        initStatus();
        status.NoData(message);
    }
    /**
     * 全屏显示无数据状态,有点击事件
     */
    protected void showNoData( String message,  String btnText, View.OnClickListener listener){
        initStatus();
        status.NoData(message,btnText,listener);
    }

    /**
     * 全屏显示无网络状态,有点击事件
     * @param listener
     */
    protected void showNoNewWork(View.OnClickListener listener){
        initStatus();
        status.NoNewWork(listener);
    }

    /**
     * 全屏显示加载中状态
     */
    protected void showLoading(){
        initStatus();
        status.Loading();
    }
    protected abstract void initTitle();
    protected abstract void initView();
    protected abstract void setData();

    /**
     * Created by yinsujun on 2016/4/1 0001 13:33
     * 设置标题及返回键
     * title:标题名称
     * id：title
     */
    protected void setTitle(String title) {
        titleView = (TitleView) rootView.findViewById(R.id.title);
        titleView.setTitle(title);
    }

    protected void onComplete(SwipeToLoadLayout refresh){
        if (refresh!=null){
            if (refresh.isRefreshing()){
                refresh.setRefreshing(false);
            }else if (refresh.isLoadingMore()){
                refresh.setLoadingMore(false);
            }
        }

    }
    protected void autoLoading(final SwipeToLoadLayout refresh,RecyclerView swipeTarget){
        //自动上拉
        swipeTarget.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                if (newState == RecyclerView.SCROLL_STATE_IDLE) {
                    if (!ViewCompat.canScrollVertically(recyclerView, 1)) {
                        refresh.setLoadingMore(true);
                    }
                }
            }
        });
    }

    public void startActivity(Class<?> cls) {
        Intent intent = new Intent(activity, cls);
        startActivity(intent);
    }

    public void startActivity(Bundle bundle, Class<?> cls) {
        Intent intent = new Intent(activity, cls);
        intent.putExtras(bundle);
        startActivity(intent);
    }


    /**
     * 显示本地或相机图片，在 url前加"file://"
     *
     * @param imageId   默认图片
     * @param url       url
     * @param imageView ImageView
     */
    protected void setImageUrl(int imageId, String url, ImageView imageView) {
        ImageLoader imageLoader = ImageLoader.getInstance();
        DisplayImageOptions options = new DisplayImageOptions.Builder()
                .showImageOnLoading(imageId) //设置图片在下载期间显示的图片
                .showImageForEmptyUri(imageId)//设置图片Uri为空或是错误的时候显示的图片
                .showImageOnFail(imageId)  //设置图片加载/解码过程中错误时候显示的图片
                .cacheInMemory(true)//设置下载的图片是否缓存在内存中
//                .cacheOnDisc(true)//设置下载的图片是否缓存在SD卡中 改成 cacheOnDisk
                .cacheOnDisk(true)//设置下载的图片是否缓存在SD卡中
                .displayer(new FadeInBitmapDisplayer(100))//是否图片加载好后渐入的动画时间
                .build();//构建完成
        imageLoader.displayImage(url, imageView, options);
    }
    protected void setImageUrl(int imageId, String url, ImageView imageView, ImageLoadingListener listener){
        ImageLoader imageLoader=ImageLoader.getInstance();
        DisplayImageOptions options = new DisplayImageOptions.Builder()
                .showImageOnLoading(imageId) //设置图片在下载期间显示的图片
                .showImageForEmptyUri(imageId)//设置图片Uri为空或是错误的时候显示的图片
                .showImageOnFail(imageId)  //设置图片加载/解码过程中错误时候显示的图片
                .cacheInMemory(true)//设置下载的图片是否缓存在内存中
//                .cacheOnDisc(true)//设置下载的图片是否缓存在SD卡中 改成 cacheOnDisk
                .cacheOnDisk(true)//设置下载的图片是否缓存在SD卡中
                .displayer(new FadeInBitmapDisplayer(100))//是否图片加载好后渐入的动画时间
                .build();//构建完成
        imageLoader.displayImage(url,imageView,options,listener);
    }





}
